package com.keon.jianbao.ui.activity;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.CompoundButton;

import com.keon.jianbao.AppContext;
import com.keon.jianbao.R;
import com.keon.jianbao.base.BaseActivity;
import com.keon.jianbao.util.DialogHelp;
import com.keon.jianbao.util.PingUtils;
import com.keon.jianbao.util.StringUtils;

import butterknife.Bind;

/**
 * Created by keon on 2015/10/9.
 */
public class JianBaoPayWayActivity extends BaseActivity implements CompoundButton.OnCheckedChangeListener {

    int appraisal_id = 0;

    @Bind(R.id.pay_way_submit)
    Button bSubmit;
    @Bind(R.id.pay_way_alipay)
    CheckBox cbAlipay;
//    @Bind(R.id.pay_way_unionpay)
//    CheckBox cbUnionpay;
    @Bind(R.id.pay_way_wxpay)
    CheckBox cbWxpay;

    int amount;

    @Override
    protected int getLayoutId() {
        return R.layout.activity_jianbao_pay_way;
    }

    @Override
    protected int getToolBarTitle() {
        return R.string.jianbao_pay_way;
    }

    @Override
    public void initView() {
        bSubmit.setOnClickListener(this);
        cbAlipay.setOnCheckedChangeListener(this);
        cbWxpay.setOnCheckedChangeListener(this);
//        cbUnionpay.setOnCheckedChangeListener(this);
    }

    @Override
    public void initData() {
        Bundle bundle = getIntent().getExtras();
//        amount = (int) bundle.getDouble("amount");
        appraisal_id = bundle.getInt("appraisal_id");
    }

    @Override
    public void onClick(View view) {
        int id = view.getId();
        switch (id) {
            case R.id.pay_way_submit:
                String type = getCheck();
                if (!StringUtils.isEmpty(type)) {
                    PingUtils pingUtils = new PingUtils(JianBaoPayWayActivity.this);
                    pingUtils.pay(appraisal_id, type, 1);
                } else {
                    showToast(R.string.jianbao_pay_way_error_null);
                }
                break;
        }
    }

    protected void onActivityResult(int requestCode, int resultCode, Intent data) {

        //支付页面返回处理
        if (requestCode == PingUtils.REQUEST_CODE_PAYMENT) {
            if (resultCode == Activity.RESULT_OK) {
                String result = data.getExtras().getString("pay_result");
                /* 处理返回值
                *"success" - payment succeed
                        * "fail" - payment failed
                        * "cancel" - user canceld
                        * "invalid" - payment plugin not installed
                        */
//                String errorMsg = data.getExtras().getString("error_msg"); // 错误信息
//                String extraMsg = data.getExtras().getString("extra_msg"); // 错误信息
//                showMsg(result, errorMsg, extraMsg);

                if (result.equals("success")) {
                    showToast("支付成功");
                    onBackPressed();
                } else if (result.equals("cancel")) {

                } else {
                    String errorMsg = data.getExtras().getString("error_msg"); // 错误信息
                    String extraMsg = data.getExtras().getString("extra_msg"); // 错误信息
                    showToast(errorMsg + extraMsg);
                }
            }
        }
    }

    void uncheckAll() {
        cbAlipay.setChecked(false);
//        cbUnionpay.setChecked(false);
        cbWxpay.setChecked(false);
    }

    void check(int id) {
        if (id != R.id.pay_way_alipay) {
            cbAlipay.setChecked(false);
        }
//        if (id != R.id.pay_way_unionpay) {
//            cbUnionpay.setChecked(false);
//        }
        if (id != R.id.pay_way_wxpay) {
            cbWxpay.setChecked(false);
        }
    }

    String getCheck() {
        if (cbAlipay.isChecked()) {
            return PingUtils.CHANNEL_ALIPAY;
//        } else if (cbUnionpay.isChecked()) {
//            return PingUtils.CHANNEL_UPMP;
        } else if (cbWxpay.isChecked()) {
            return PingUtils.CHANNEL_WECHAT;
        }
        return "";
    }

    @Override
    public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
        int id = buttonView.getId();
        if (isChecked == true) {
            check(id);
        }
    }

    public void showMsg(String title, String msg1, String msg2) {
        String str = title;
        if (null != msg1 && msg1.length() != 0) {
            str += "\n" + msg1;
        }
        if (null != msg2 && msg2.length() != 0) {
            str += "\n" + msg2;
        }
        DialogHelp.getMessageDialog(AppContext.getContext(), str).show();
    }
}
